import requests as re
import parsel
import csv
import time

with open("Cookie.txt", mode='r') as f:
    cookies = f.read()
movie_list = [35426925, 36149032, 35350109, 36467351, 26608246, 35927496, 36212631, 35169716, 35712804, 35902857,
              3541415, 1889243, 1292064, 2131459, 1851857]
for movie in movie_list:
    for page in range(0, 600, 20):
        url = f'https://movie.douban.com/subject/{movie}/comments?start={page}&limit=20&status=P&sort=new_score'
        headers = {
            "Cookie": cookies,
            "User-Agent":
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 "
                "Safari/537.36 Edg/115.0.1901.188 "
        }
        url_html = re.get(url, headers=headers).text
        url_html.encode(encoding='utf-8')
        selector = parsel.Selector(url_html)
        comment_list1 = selector.css(".comment-item")
        for comment in comment_list1:
            name = comment.css(".comment-info a::text").get()
            short = comment.css(".short::text").get().strip()
            time1 = comment.css(".comment-time::text").get().strip()
            location = comment.css(".comment-location::text").get()
            print(name, short, time1, location)
            with open("Top10电影评论.csv", mode='a', encoding="utf-8", newline='') as f:
                writer_csv = csv.writer(f)
                writer_csv.writerow([name, short, time1, location])
        time.sleep(2)

time.sleep(2)
